package com.dz.service.impl;

import com.dz.constant.MessageConstant;
import com.dz.entity.ShoppingCart;
import com.dz.mapper.ShoppingCartMapper;
import com.dz.service.ShoppingCartService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import io.jsonwebtoken.lang.Assert;

import java.util.List;

/**
 * @author 鄧浩奇
 * @classname ShoppingCartServiceImpl
 * @description
 * @since 2024/4/5 16:29
 **/

@Service
@RequiredArgsConstructor
public class ShoppingCartServiceImpl implements ShoppingCartService {

    private final ShoppingCartMapper shoppingCartMapper;

    /*
     * 添加购物车
     * */
    @Override
    public Integer addShoppingCart(ShoppingCart shoppingCart) {
        Integer rows = shoppingCartMapper.insertShoppingCart(shoppingCart);
        return rows > 0 ? rows : 0;
    }

    /*
     * 查询购物车
     * */
    @Override
    public List<ShoppingCart> getList(Long userID) {
        Assert.notNull(userID, MessageConstant.PARAM_ERROR);
        return shoppingCartMapper.selectListByUserId(userID);
    }


    /*
     * 删除购物车中的一条记录
     * */
    @Override
    public Integer remove(ShoppingCart s) {
        Assert.notNull(s,MessageConstant.PARAM_ERROR);
        Long id = s.getId();
        if (s.getNumber() > 1) {
            return shoppingCartMapper.updateNumber(id, false);
        }
        return shoppingCartMapper.deleteOneById(id);
    }



    /*
    * 清空购物车
    * */
    @Override
    public Integer cleanByUserId(Long userId) {
        Assert.notNull(userId,MessageConstant.PARAM_ERROR);
        return shoppingCartMapper.deleteAllByUserId(userId);
    }

}
